set nu
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set cinoptions=g0
set colorcolumn=81
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
set nocompatible            " 关闭 vi 兼容模式
syntax on                   " 自动语法高亮
"set paste                   " 取消粘贴自动注释
set number                  " 显示行号
set cursorline              " 突出显示当前行
set ruler                   " 打开状态栏标尺
set nobackup                " 覆盖文件时不备份
set autochdir               " 自动切换当前目录为当前文件所在的目录
filetype plugin indent on   " 开启插件
set backupcopy=yes          " 设置备份时的行为为覆盖
set hlsearch                " 搜索时高亮显示被找到的文本
set noerrorbells            " 关闭错误信息响铃
set novisualbell            " 关闭使用可视响铃代替呼叫
set t_vb=                   " 置空错误铃声的终端代码
set showmatch               " 插入括号时，短暂地跳转到匹配的对应括号
set matchtime=1             " 短暂跳转到匹配括号的时间
set magic                   " 设置魔术
set guioptions-=T           " 隐藏工具栏
set guioptions-=m           " 隐藏菜单栏
set completeopt=menu        " 关闭scratch 预览窗口
set backspace=indent,eol,start
set cmdheight=1             " 设定命令行的行数为
set t_Co=256                " 让xshell 也支持vim的256色配色方案
set tags=./.tags;,.tags
" set tags+=../.tag
" set tags+=../../.tag
" set tags+=../../../.tag
" set tags+=../*/.tag
" set tags+=../*/*/.tag
" set tags+=../../*/*/.tag
set foldcolumn=0
setlocal foldlevel=1
"set foldclose=all " 设置为折叠
set nofoldenable  " 设置为自动关闭折叠
set laststatus=2      " 总是显示状态栏
"nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
" 用空格键来开关折叠
"让vim记忆上次编辑的位置
autocmd BufReadPost *
            \ if line("'\"")>0&&line("'\"")<=line("$") |
            \ exe "normal g'\"" |
            \ endif
"让vim记忆上次编辑的位置
" 警戒线颜色设置
highlight ColorColumn ctermfg=green ctermbg=red
highlight LineNr cterm=none ctermfg=green ctermbg=darkblue
highlight SpellBad ctermfg=15 ctermbg=1 guifg=White guibg=Red
" 搜索背景色设置
highlight Search cterm=none ctermfg=yellow ctermbg=red
" 下拉菜单背景色设置
highlight Pmenu ctermbg=green
" 下拉菜单选择行的背景色设置
highlight PmenuSel ctermbg=red ctermfg=yellow
" vimdiff 背景色设置
highlight DiffAdd     term=bold ctermfg=232 ctermbg=214
highlight DiffChange  term=bold ctermbg=248
highlight DiffDelete  term=reverse ctermfg=15 ctermbg=2
highlight DiffText    term=reverse ctermfg=15 ctermbg=1 gui=undercurl guifg=White guibg=Red guisp=Red
"highlight DiffText    term=reverse cterm=bold ctermbg=9

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" 让vundle管理插件版本,必须
Plugin 'VundleVim/Vundle.vim'
" 自动生成ctags索引
Plugin 'ludovicchabant/vim-gutentags'
" 提供ctags数据库无缝切换
Plugin 'skywind3000/gutentags_plus'
" vim支持markdown
"Plugin 'plasticboy/vim-markdown'
" vim markdown 生成表格
Plugin 'dhruvasagar/vim-table-mode'
" 配合snippets自动补全, 没有的话snippets不生效
Plugin 'SirVer/ultisnips'
" 支持代码片段
Plugin 'honza/vim-snippets'
" 状态栏显示不同颜色
Plugin 'Lokaltog/vim-powerline'
" 记录历史变量名或路径或符号，使用tab即可循环选择
Plugin 'ervandew/supertab'
" 支持语法检测和编码风格检测
Plugin 'scrooloose/syntastic'
" 设置对齐线
Plugin 'Yggdroot/indentLine'
" git项目编辑后在行前显示状态~ + - 等
Plugin 'airblade/vim-gitgutter'
" 代码对齐
Plugin 'godlygeek/tabular'
" nerdtree
Plugin 'scrooloose/nerdtree'
" git管理的项目在项目窗口中的状态标识
Plugin 'git://github.com/Xuyuanp/nerdtree-git-plugin.git'
" 文件查找工具，使用\ t查找文件
Plugin 'wincent/command-t'
" vim 状态栏插件
Plugin 'bling/vim-airline'
" 代码格式化插件
Plugin 'Chiel92/vim-autoformat'
Plugin 'rdnetto/YCM-Generator'
" 代码补全
Plugin 'Valloric/YouCompleteMe'

call vundle#end()            " 必须
filetype plugin indent on    " 必须 加载vim自带和插件相应的语法和文件类型相关脚本

autocmd BufNewFile  *.c,*.cpp  0r ~/.vim/skeleton/c.ske
autocmd BufNewFile  *.h,*.hpp  0r ~/.vim/skeleton/h.ske
autocmd BufNewFile  *.sh       0r ~/.vim/skeleton/sh.ske
autocmd BufNewFile * normal G
autocmd BufNewFile *.[ch],*.sh,*.hpp,*.cpp ks|call CreatedTime()|'
fun CreatedTime()
    if line("$") > 20
        let l = 20
    else
        let l = line("$")
    endif
    exe "1," . l . "g/Date       :.*/s//Date       : " .strftime("%c")
endfun

" 保存时自动删除行尾空白字符
function! StripTrailingWhitespace()
    normal mZ
    %s/\s\+$//e
    if line("'Z") != line(".")
        echo "Stripped whitespace\n"
    endif
    normal `Z
endfunction
autocmd BufWritePre *.cpp,*.hpp,*.h :call StripTrailingWhitespace()

" 设置nerd窗口中git文件状态
let g:NERDTreeIndicatorMapCustom = {
    \ "Modified"  : "✹",
    \ "Staged"    : "✚",
    \ "Untracked" : "✭",
    \ "Renamed"   : "➜",
    \ "Unmerged"  : "═",
    \ "Deleted"   : "✖",
    \ "Dirty"     : "✗",
    \ "Clean"     : "✔︎",
    \ "Unknown"   : "?"
    \ }

" 自动关闭选项预览窗口
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
autocmd CursorMovedI * if pumvisible() == 0|pclose|endif

"YouCompleteMe相关设置
"let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
let g:ycm_global_ycm_extra_conf='~/.ycm_extra_conf.py'
"开启语义补全
let g:ycm_seed_identifiers_with_syntax=1
"不显示开启vim时检查ycm_extra_conf文件的信息
let g:ycm_confirm_extra_conf=0
"每次重新生成匹配项，禁止缓存匹配项
let g:ycm_cache_omnifunc=0
" 从第2个键入字符就开始罗列匹配项
let g:ycm_min_num_of_chars_for_completion=2                 "
"不查询ultisnips提供的代码模板补全，如果需要，设置成1即可
let g:ycm_use_ultisnips_completer=0
let g:ycm_key_list_select_completion = ['<c-n>', '<Down>']
let g:ycm_enable_diagnostic_signs = 0
let g:ycm_enable_diagnostic_highlighting = 0

"设置对齐线的颜色
let g:indentLine_color_term = 239
" 设置
let g:airline#extensions#tabline#enabled = 1
let NERDTreeWinPos=1   " 右侧显示

" 设置markdown的table模式下使用|进行表格制作
let g:table_mode_corner='|'

" gutentags 搜索工程目录的标志，碰到这些文件/目录名就停止向上一级目录递归
let g:gutentags_project_root = ['.root', '.svn', '.git', '.hg', '.project']

" 所生成的数据文件的名称
let g:gutentags_ctags_tagfile = '.tags'

" 将自动生成的 tags 文件全部放入 ~/.cache/tags 目录中，避免污染工程目录
let s:vim_tags = expand('~/.cache/tags')
let g:gutentags_cache_dir = s:vim_tags
" 检测 ~/.cache/tags 不存在就新建 "
if !isdirectory(s:vim_tags)
    silent! call mkdir(s:vim_tags, 'p')
endif

" 配置 ctags 的参数
let g:gutentags_ctags_extra_args = ['--fields=+niazS', '--extra=+q']
let g:gutentags_ctags_extra_args += ['--c++-kinds=+px']
let g:gutentags_ctags_extra_args += ['--c-kinds=+px']

" 设置NerdTree F3调出
map <F3> :NERDTreeMirror<CR>
map <F3> :NERDTreeToggle<CR>
" 设置f4为代码自动格式化，配合插件vim-autoformat使用
noremap <F4> :Autoformat<CR>
" 设置f9打开或关闭对齐线
map <F9> :IndentLinesToggle<CR>
" 设置,2 ,3 ,4 分别为vimdiff的获取2 3 4 窗口内容, 并更新vimdiff
map <silent> ,2 :diffget 2<CR> :diffupdate<CR>
map <silent> ,3 :diffget 3<CR> :diffupdate<CR>
map <silent> ,4 :diffget 4<CR> :diffupdate<CR>

autocmd BufEnter * if &filetype == "" | setlocal ft=cpp | endif
nnoremap <leader>jd :YcmCompleter GoToDeclaration<CR>

"inoremap ' ''<ESC>i
"inoremap " ""<ESC>i
" inoremap ( ()<ESC>i
" inoremap [ []<ESC>i
" inoremap { {<CR>}<ESC>O
